package com.lebang.service.buss;

import com.lebang.entity.buss.FinanceRecord;
import com.lebang.entity.task.TaskComments;
import com.lebang.example.FinanceRecordExample;
import com.lebang.mapper.FinanceRecordMapper;
import com.lebang.model.Whether;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hwq
 */
@Service
public class FinanceRecordService {
    @Autowired
    private FinanceRecordMapper financeRecordMapper;

    public JsonData insert(FinanceRecord po) {
        po.setHasDeleted(Whether.NO.getDbIndex());
        po.setCreateTime(new Date());
        po.setModifyTime(new Date());
        BigDecimal outlay=financeRecordMapper.checkAmoutByTaskId(po.getTaskId(),1);//收款
        BigDecimal cash=financeRecordMapper.checkAmoutByTaskId(po.getTaskId(),2);//退款
        if(po.getCat()==1){
            outlay=outlay.add(po.getAmount());
        }else {
            cash=cash.add(po.getAmount());
        }
        if(outlay.compareTo(cash)==-1){
            return Callback.fail("200", "收款金额必须大于等于退款金额", null);
        }
        int init = financeRecordMapper.insertSelective(po);

        if (init > 0) {

            return Callback.success("200", "新增成功", null);
        } else {

            return Callback.fail("403", "新增失败", null);
        }
    }


    public JsonData update(FinanceRecord po) {
        po.setModifyTime(new Date());
        int init = financeRecordMapper.updateByPrimaryKeySelective(po);
        if (init > 0) {

            return Callback.success("200", "修改成功", null);
        } else {

            return Callback.fail("403", "修改失败", null);
        }
    }


    public FinanceRecord selectByPrimaryKey(Integer id) {
        return financeRecordMapper.selectByPrimaryKey(id);
    }

    public int confirm(List<String> list, String userid,String  note) {

        return financeRecordMapper.confirmByList(list, userid,note);
    }

    public int cancel(List<String> list, String userid) {
        return financeRecordMapper.cancelByList(list, userid);
    }

    public Map<String, Object> missionExpenditurePage(Map<String, Object> list) {

        Map<String, Object> result = new HashMap<>(1);
        result.put("data", financeRecordMapper.missionExpenditurePage(list));
        result.put("count", financeRecordMapper.missionExpenditureCount(list));
        result.put("code", 0);
        return result;
    }

    public List<Map<String, Object>> findpage(int startNum, Integer limit, FinanceRecord financeRecord, String name, String star, String end) {
        return financeRecordMapper.findpage(startNum,limit,financeRecord,name,star,end);
    }

    public Long findcout(FinanceRecord financeRecord, String name, String star, String end) {
        return financeRecordMapper.findcout(financeRecord,name,star,end);
    }

    public Long count(FinanceRecordExample example) {
        return financeRecordMapper.countByExample(example);
    }

    public List<FinanceRecord> selectAll(FinanceRecordExample example) {
        return financeRecordMapper.selectByExample(example);
    }
}
